import { login, logout, getInfo, changePassword, register, getFile, getImg, train } from '@/api/user'
import { getModelInfo, getDatasetInfo } from '@/api/user'
import { getToken, setToken, removeToken } from '@/utils/auth'
import { resetRouter } from '@/router'
import Cookies from 'js-cookie'
// import { get } from 'sortablejs'

const getDefaultState = () => {
  return {
    token: getToken(),
    name: '',
    avatar: '',
    introduction: '',
    roles: [],
    // the rolese of users
    usertype: -1,
    timeline: undefined
  }
}

const state = getDefaultState()

const mutations = {
  RESET_STATE: (state) => {
    Object.assign(state, getDefaultState())
  },
  SET_TOKEN: (state, token) => {
    state.token = token
  },
  SET_INTRODUCTION: (state, introduction) => {
    state.introduction = introduction
  },
  SET_NAME: (state, name) => {
    state.name = name
  },
  SET_AVATAR: (state, avatar) => {
    state.avatar = avatar
  },
  SET_ROLES: (state, roles) => {
    state.roles = roles
  },
  // set the role of user
  SET_USERTYPE: (state, usertype) => {
    state.usertype = usertype
  }
}

const actions = {
  // user login
  login({ commit }, userInfo) {
    const { username, password, usertype } = userInfo
    // console.log(userInfo)
    console.log(username, password)
    return new Promise((resolve, reject) => {
      console.log('request', username, password)

      login({ username: username.trim(), password: password, usertype: usertype }).then(response => {
        console.log(response)
        if (response.code === 1000) {
          console.log('1000', response)
          // const { data } = response
          console.log(response.token)
          Cookies.set('username', username.trim())
          console.log(response.data.token)
          commit('SET_TOKEN', response.data.token)
          commit('SET_USERTYPE', usertype)
          commit('SET_NAME', username.trim())
          setToken(response.data.token)
          resolve()
        }
      }).catch(error => {
        // const statusCode = error.response.status;
        console.log('error!!!!!!!' + error.response)
        reject(error)
      })
    })
  },

  // get user info
  getInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getInfo(state.token).then(response => {
        console.log(response)
        const { data } = response

        if (!data) {
          return reject('Verification failed, please Login again.')
        }

        const { roles, name, avatar, introduction } = data

        commit('SET_ROLES', [roles])
        commit('SET_NAME', name)
        commit('SET_AVATAR', avatar)
        commit('SET_INTRODUCTION', introduction)
        resolve(data)
      }).catch(error => {
        reject(error)
      })
    })
  },

  // user logout
  logout({ commit, state }) {
    return new Promise((resolve, reject) => {
      logout(state.token).then(() => {
        removeToken() // must remove  token  first
        resetRouter()
        commit('RESET_STATE')
        resolve()
      }).catch(error => {
        reject(error)
      })
    })
  },

  // remove token
  resetToken({ commit }) {
    return new Promise(resolve => {
      removeToken() // must remove  token  first
      commit('RESET_STATE')
      resolve()
    })
  },

  register({ commit }, userInfo) {
    const { newUsername, newPassword, usertype } = userInfo
    return new Promise((resolve, reject) => {
      register({ newUsername: newUsername.trim(), newPassword: newPassword, usertype: usertype }).then(response => {
        if (response.code === 1000) {
          resolve(response)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  changePassword({ commit }, userInfo) {
    const { username, oldPassword, newPassword } = userInfo
    return new Promise((resolve, reject) => {
      changePassword({ username: username.trim(), oldPassword: oldPassword, newPassword: newPassword }).then(response => {
        if (response.code === 1000) {
          resolve(response)
        }
      }).catch(error => {
        reject(error)
      })
    })
  },

  getModelInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getModelInfo().then(response => {
        // console.log(response)
        resolve(response.body)
      }).catch(error => {
        reject(error)
      })
    })
  },

  getDatasetInfo({ commit, state }) {
    return new Promise((resolve, reject) => {
      getDatasetInfo().then(response => {
        resolve(response.body)
      }).catch(error => {
        reject(error)
      })
    })
  },

  getFILE({ commit, state }, fileName) {
    return new Promise((resolve, reject) => {
      getFile(fileName).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  getIMG({ commit, state }, fileName) {
    return new Promise((resolve, reject) => {
      getImg(fileName).then(response => {
        resolve(response)
      }).catch(error => {
        reject(error)
      })
    })
  },

  training({ commit }, params) {
    return new Promise((resolve, reject) => {
      train({ lr: params.lr, epoch: params.epoch, train_path: 'dataset/train.txt' }).then(response => {
        console.log(response)
        resolve(response)
      }).catch(error => {
        console.log('error!!!!!!!' + error.response)
        reject(error)
      })
    })
  }
}

export default {
  namespaced: true,
  state,
  mutations,
  actions
}

